package com.crayon.scoreboard.views;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.TypedArray;
import android.database.ContentObserver;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Handler;
import android.os.SystemClock;
import android.provider.Settings;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewDebug;
import android.widget.TextView;

import com.crayon.scoreboard.R;

import java.util.Calendar;
import java.util.TimeZone;

/**
 * com.crayon.scoreboard.views.TextClockEx
 * Created by kafka on 15. 2. 17..
 */
public class TextClockEx extends TextView {


    private CharSequence mFormat="%d:%d:%d";

    private Calendar mTime;
    private boolean mHasSeconds=true;


    private final Runnable mTicker = new Runnable() {
        public void run() {

            long startTime = mTime.getTimeInMillis();
            long now = System.currentTimeMillis();

            long spendtime = now -startTime;


            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(spendtime);

            int time = (int) spendtime/(60*60*1000);
            int minute =(int) spendtime/(60*1000);
            int second =(int) (spendtime%(60*1000))/1000;
            setText(String.format("%02d :%02d : %02d",time,minute,second));

//            getHandler().postAtTime(mTicker, 100);
            getHandler().postDelayed(mTicker,500);
        }
    };


    public TextClockEx(Context context, AttributeSet attrs) {
        super(context,attrs);
        init();
    }


    private void init() {
        createTime(null);
        // Wait until onAttachedToWindow() to handle the ticker
        setTextColor(Color.YELLOW);
        setBackgroundColor(Color.BLACK);
        setTypeface(null, Typeface.BOLD);
        setGravity(Gravity.CENTER);

        setTextSize(getResources().getDimension(R.dimen.textTimeCount));

    }

    private void createTime(String timeZone) {
            mTime = Calendar.getInstance();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        createTime(null);
        mTicker.run();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        getHandler().removeCallbacks(mTicker);
    }
    Calendar timePauseStat;
    public void setPause(){
        timePauseStat = Calendar.getInstance();
        getHandler().removeCallbacks(mTicker);
    }
    public void setRestart(){
        long loseTime =System.currentTimeMillis()- timePauseStat.getTimeInMillis();
        mTime.setTimeInMillis(mTime.getTimeInMillis()+loseTime);
        mTicker.run();
    }
    public void setReset(){
        createTime(null);
        mTicker.run();
    }


}
